package com.xjm.service.serviceImpl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.xjm.excption.AppException;
import com.xjm.mapper.ConfigMapper;
import com.xjm.service.ConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ConfigServiceImpl implements ConfigService {

    private final ConfigMapper configMapper;

    @Override
    public JSON get(String key) {
        return JSONUtil.parse(configMapper.get(key));
    }

    @Override
    public Boolean set(String key, JSON value) {
        if (get(key) != null) {
            throw new AppException("该配置已存在");
        }
        return configMapper.set(key, JSONUtil.toJsonStr(value)) != 0;
    }

    @Override
    public Boolean update(String key, JSON value) {
        return configMapper.update(key, JSONUtil.toJsonStr(value)) != 0;
    }

    @Override
    public Boolean delete(String key) {
        return configMapper.delete(key) != 0;
    }
}
